package org.jeecg.trace.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.jeecg.common.api.vo.Result;
import org.jeecg.report.dto.QisSpcChartDataDTO;
import org.jeecg.report.service.IQisSpcService;
import org.jeecg.report.vo.QisSpcParameterVO;
import org.jeecg.trace.dto.*;
import org.jeecg.trace.entity.MesUnitHistory;
import org.jeecg.trace.mapper.SampleTraceMapper;
import org.jeecg.trace.service.IMesUnitHistoryService;
import org.jeecg.trace.service.SampleTraceService;
import org.jeecg.trace.vo.DictVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@Service
@Slf4j
public class SampleTraceServiceImpl implements SampleTraceService {
    @Autowired
    private IMesUnitHistoryService iMesUnitHistoryService;
    @Autowired
    private SampleTraceMapper sampleTraceMapper;
    @Autowired
    private IQisSpcService iQisSpcService;
    @Override
    public SampleTraceDTO queryList(Page<MesUnitHistory> page, QisSpcParameterVO map) {
        SampleTraceDTO sampleTraceDTO = new SampleTraceDTO();
        if (Objects.nonNull(map.getUnitsn())){
            Integer result = sampleTraceMapper.getSampleTyp(map.getUnitsn());
            //查询样本点检历史列表
            LambdaQueryWrapper<MesUnitHistory> wrapper = new LambdaQueryWrapper<MesUnitHistory>()
                    .eq(MesUnitHistory::getUnitsn, map.getUnitsn())
                    .ge(Objects.nonNull(map.getSDate()),MesUnitHistory::getCtime,map.getSDate())
                    .le(Objects.nonNull(map.getEDate()),MesUnitHistory::getCtime,map.getEDate())
                    .orderByDesc(MesUnitHistory::getCtime);
            IPage<MesUnitHistory> historyPage = iMesUnitHistoryService.page(page, wrapper);
            sampleTraceDTO.setHistoryList(historyPage);
            if (Objects.nonNull(map.getRecId())) {
                QisSpcChartDataDTO sampleSPCData = getSampleSPCData(map);
                sampleTraceDTO.setQisSpcChartDataDTO(sampleSPCData);
            }
            return sampleTraceDTO;
        }else {
            return sampleTraceDTO;
        }
    }

    public QisSpcChartDataDTO getSampleSPCData(QisSpcParameterVO map){
        Result<QisSpcChartDataDTO> chartInfo = iQisSpcService.getChartInfo(map);
        return chartInfo.getResult();
    }

    @Override
    public void exportXls(HttpServletRequest request, HttpServletResponse response, Map<String, String> map){
        //定义easyExcel导出的对象
        ExcelWriter excelWriter = null;
        //内容样式策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //垂直居中,水平居中
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
        contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
        contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
        contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
        // 字体策略
        WriteFont contentWriteFont = new WriteFont();
        // 字体大小
        contentWriteFont.setFontHeightInPoints((short) 8);
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        //头策略使用默认 设置字体大小
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 10);
        headWriteCellStyle.setWriteFont(headWriteFont);
        try {
            excelWriter = EasyExcel.write(response.getOutputStream()).build();
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-disposition", "attachment;filename=demo.xlsx");
            ExcelWriterSheetBuilder procHistory = EasyExcel.writerSheet(0, "产品加工历史");
            procHistory.head(SampleExportDTO.class)
                    .registerWriteHandler(new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle));
            //查询数据并写入
            List<SampleExportDTO> list = sampleTraceMapper.exportXls(map);
            excelWriter.write(list, procHistory.build());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }

    @Override
    public List<DictVO> queryProcList(Map<String, String> map) {
        return sampleTraceMapper.queryProcList(map);
    }
}
